package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemDatabaseColumnBo;
import com.cw.jeeyt.service.bo.system.SystemProjectInfoBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemDatabaseColumnService;
import com.cw.jeeyt.service.intf.system.SystemProjectInfoService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库字段信息控制类
 * @author chenw
* @version 1.0.0
 */
@Controller
@RequestMapping("/systemDatabaseColumn")
public class SystemDatabaseColumnController extends BaseController {
    @Resource
    private SystemDatabaseColumnService systemDatabaseColumnService;
    @Resource
    private SystemProjectInfoService systemProjectInfoService;

    /**
     * 跳转到列表查询页面
     *
     * @param systemDatabaseColumnBo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryIndex")
    public ModelAndView queryIndex(SystemDatabaseColumnBo systemDatabaseColumnBo, String databaseId) {
        ModelAndView mv = new ModelAndView("system/systemDatabaseColumn_queryIndex");
        mv.addObject("tableId", systemDatabaseColumnBo.getTableId());
        mv.addObject("databaseId", databaseId);
        return mv;
    }

    /**
     * 跳转到列表查询页面
     *
     * @param systemDatabaseColumnBo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(SystemDatabaseColumnBo systemDatabaseColumnBo, String databaseId) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemDatabaseColumn_index");
        mv.addObject("tableId", systemDatabaseColumnBo.getTableId());
        mv.addObject("databaseId", databaseId);
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemDatabaseColumn")
    public ResultData listSystemDatabaseColumn(@RequestBody DataTable dataTable, String tableId,
        HttpServletResponse response) throws Exception {

        SystemDatabaseColumnBo systemDatabaseColumnBo = JSONObject.parseObject(
            JSON.toJSONString(dataTable.getQueryCond()), SystemDatabaseColumnBo.class);
        if (systemDatabaseColumnBo == null) {
            systemDatabaseColumnBo = new SystemDatabaseColumnBo();
        }
        systemDatabaseColumnBo.setTableId(tableId);
        PageInfo<SystemDatabaseColumnBo> pageInfo = systemDatabaseColumnService.queryByPage(systemDatabaseColumnBo,
            dataTable.getStart(), dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView toForm(String id, String operation) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemDatabaseColumn_form");
        SystemDatabaseColumnBo systemDatabaseColumnBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemDatabaseColumnBo = new SystemDatabaseColumnBo();
        } else {
            systemDatabaseColumnBo = systemDatabaseColumnService.getSystemDatabaseColumnById(id);
        }
        mv.addObject("systemDatabaseColumn", systemDatabaseColumnBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 根据操作类型保存数据
     *
     * @param systemDatabaseColumnBo
     * @param operation add新增  edit修改 view查看
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemDatabaseColumn", method = RequestMethod.POST)
    public JsonResultDto saveSystemDatabaseColumn(SystemDatabaseColumnBo systemDatabaseColumnBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemDatabaseColumnService.insertSystemDatabaseColumn(systemDatabaseColumnBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemDatabaseColumnService.updateSystemDatabaseColumn(systemDatabaseColumnBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
        }
        jsonResultDto.setOperation(operation);
        return jsonResultDto;
    }

    /**
     * 删除数据库字段信息
     *
     * @param systemDatabaseColumnBo
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto deleteSystemDatabaseColumn(SystemDatabaseColumnBo systemDatabaseColumnBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemDatabaseColumnService.deleteSystemDatabaseColumn(systemDatabaseColumnBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 跳转到同步数据条目
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-syncQuery")
    public ModelAndView toSyncQyery(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemDatabaseColumn_syncIndex");
        List<SystemProjectInfoBo> systemProjectInfoList = systemProjectInfoService.querySystemProjectInfoList();
        mv.addObject("systemProjectInfoList", systemProjectInfoList);
        String selectType = request.getParameter("selectType");
        String moduleId = request.getParameter("moduleId");
        mv.addObject("selectType", selectType);
        mv.addObject("moduleId", moduleId);
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listImportSystemDatabaseColumn")
    public ResultData listImportSystemDatabaseColumn(@RequestBody DataTable dataTable) throws Exception {

        SystemDatabaseColumnBo systemDatabaseColumnBo = JSONObject.parseObject(
            JSON.toJSONString(dataTable.getQueryCond()), SystemDatabaseColumnBo.class);
        if (systemDatabaseColumnBo == null) {
            systemDatabaseColumnBo = new SystemDatabaseColumnBo();
        }
        PageInfo<SystemDatabaseColumnBo> pageInfo = systemDatabaseColumnService.queryImportByPage(
            systemDatabaseColumnBo, dataTable.getStart(), dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }
}
